﻿/// <reference path="~/Scripts/Library/jquery-1.9.1.min.js" />

function LastResponseMessage_ClearError() {

    var lastResponseMessageLabel = $("#LastResponseMessageLabel");

    lastResponseMessageLabel.html("");

    lastResponseMessageLabel.parent().removeClass("ui-state-error");

    return;

}

function LastResponseMessage_SetError(errorMessage) {

    var lastResponseMessageLabel = $("#LastResponseMessageLabel");

    lastResponseMessageLabel.html(errorMessage);

    lastResponseMessageLabel.parent().addClass("ui-state-error");

    return;

}

function Validate() {

    var isValid = true;


    // RESET ALL FIELD CHANGES 

    $(".ui-state-error").removeClass("ui-state-error");

    LastResponseMessage_ClearError();


    // ALLOW THE ERRORS TO ACCUMULATE FROM LEAST TO MOST IMPORTANT

    if ($("#Insurer_Name").val() === "") {

        LastResponseMessage_SetError("The Name must not be empty.");

        $("#Insurer_Name_Label").parent().addClass("ui-state-error");

        $("#PropertyTabs>ul>li").first().addClass("ui-state-error");

        isValid = false;

    }


    return isValid;

}



function OkApply_OnClick(sender) {

    var isValid = Validate();

    if (!isValid) { return; }


    LastResponseMessage_ClearError();

    var lastResponseMessageLabel = $("#LastResponseMessageLabel");

    lastResponseMessageLabel.html("Saving...");

    $("#AjaxWorking").show();


    $("#ButtonContainer button").attr("disabled", "disabled");

    if (typeof ConfigurationItem_OnBeforeSave == "function") {

        isValid = ConfigurationItem_OnBeforeSave();

        if (!isValid) { return; }

    }



    var postbackUrl = $("#RootUrl").val() + "Insurer/InsurerSave/" + $("#Insurer_Id").val();

    $.ajax({

        type: "POST",

        url: postbackUrl,

        data: $("#CoreObjectItemForm").serialize(),

        error: function (jqXHR, textStatus, errorThrown) {

            alert("Error performing AJAX page request: " + errorThrown + " [Url:" + postbackUrl + "]");

            LastResponseMessage_SetError(errorThrown);

        },

        success: function (response) {

            if (response.Result) {

                LastResponseMessage_ClearError();

                $("#LastResponseMessageLabel").text("Save successful.");

                // CALL REFRESH ON PARENT (HOME) PAGE 

                if (window.opener) {

                    if (typeof window.opener.ContentGrid_Refresh == "function") {

                        window.opener.ContentGrid_Refresh();

                    }

                }

                var newLocation = window.location.toString();

                newLocation = newLocation.substr(0, newLocation.indexOf("/Insurer/Insurer") + ("/Insurer/Insurer").length);

                // USE THE EXISTING PLAN TYPE ID TO SEE IF THE ORIGINAL WAS 0

                if ((parseInt($("#Insurer_Id").val()) == 0) && ($(sender).text() == "Apply")) {

                    window.location = newLocation + "/" + response.Id;

                }

                else if ($(sender).text() == "OK") {

                    window.close();

                }

            }

            else {

                LastResponseMessage_SetError(response.ExceptionMessage);

            }

        },

        complete: function () {

            $("button").removeAttr("disabled");

            if (typeof ConfigurationItem_OnSaveComplete == "function") {

                ConfigurationItem_OnSaveComplete();

            }

            $("#AjaxWorking").hide();

        }

    });

}

function Cancel_OnClick(sender) {

    window.close();

}


function Name_OnChange() {

    var itemName = $("#Insurer_Name").val();

    if (itemName.length == 0) {

        $("#InsurerNameLabel").text("(no name assigned)");

    }

    else {

        $("#InsurerNameLabel").text(itemName);

    }

}

$(document).ready(function () {
    
    // REPURPOSE THE APPLICATION TITLE BAR FROM THE MASTER VIEW

    $("#ApplicationTitleBar").removeClass();

    $("#ApplicationTitleBar").empty();

    $("#EntityTitleBar").appendTo("#ApplicationTitleBar");


    $("#ApplicationContent").removeClass();

    $("#ApplicationContent").addClass("ui-widget-content");


    // INITIALIZE NAME 

    Name_OnChange();



    $("button").button();


    // SET UP AJAX PROGRESS BAR 

    ProgressBar_AjaxUnder_Initialize("AjaxWorking_ProgressBar");

    $("#AjaxWorking_ProgressBar").show();


    // ADJUST THE BUTTON CONTAINER HEIGHT TO FILL TO END OF PAGE

    // THE HEIGHT NEEDS TO VERY BASED ON THE BOX MODEL OF THE BROWSER 

    var heightAdjustment = 650 - parseInt($("body").height());

    var windowAdjustment = parseInt($(window).height()) - 650;

    $("#ButtonContainer").height(parseInt($("#ButtonContainer").innerHeight()) + heightAdjustment + windowAdjustment);

});
